package com.wsh.sms.controller;

import com.wsh.sms.entity.MonthSalary;
import com.wsh.sms.service.MonthSalaryService;
import com.wsh.sms.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wsh
 */
@RestController
@RequestMapping("/salary")
@CrossOrigin
public class MonthSalaryController {

    private final MonthSalaryService monthSalaryService;

    @Autowired
    public MonthSalaryController(MonthSalaryService monthSalaryService){
        this.monthSalaryService = monthSalaryService;
    }

    @RequestMapping(value = "/employee-salary")
    public List<MonthSalary> getMonthSalaryByNumber(@RequestParam(name = "number") String number){
        return monthSalaryService.getMonthSalaryByNumber(number);
    }

    @RequestMapping("/month-employee-salary")
    public MonthSalary getOneMonthSalaryByNumber(
            @RequestParam(name = "number") String number,
            @RequestParam(name = "year") int year,
            @RequestParam(name = "month") int month){
        return monthSalaryService.getOneMonthSalaryByNumber(number,year,month);
    }

    @RequestMapping(value = "/update-employee-salary")
    public Result updateEmployeeSalary(
            @RequestParam(name = "number") String number,
            @RequestParam(name = "year") int year,
            @RequestParam(name = "month") int month,
            @RequestParam(name = "basicSalary") double basicSalary,
            @RequestParam(name = "percentage") double percentage,
            @RequestParam(name = "subsidy") double subsidy,
            @RequestParam(name = "lastYear") String lastYear,
            @RequestParam(name = "lastMonth") String lastMonth
    ){
        int result = 0;
        try{
            result = monthSalaryService.updateEmployeeSalary(number,year,month,basicSalary,percentage,subsidy,lastYear,lastMonth);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (result >= 1){
            return new Result<>(200,"success","更新成功");
        }
        else {
            return new Result<>(200,"fail","更新失败");
        }
    }

    @RequestMapping(value = "/add-employee-salary")
    public Result addEmployeeSalary(
            @RequestParam(name = "number") String number,
            @RequestParam(name = "year") int year,
            @RequestParam(name = "month") int month,
            @RequestParam(name = "basicSalary") double basicSalary,
            @RequestParam(name = "percentage") double percentage,
            @RequestParam(name = "subsidy") double subsidy
    ){
        int result = 0;
        try{
            result = monthSalaryService.addMonthSalary(number,year,month,basicSalary,percentage,subsidy);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (result >= 1){
            return new Result<>(200,"success","添加成功");
        }
        else {
            return new Result<>(200,"fail","添加失败");
        }
    }

    @RequestMapping(value = "/delete-employee-salary")
    public Result deleteEmployeeSalary(
            @RequestParam(name = "number") String number,
            @RequestParam(name = "year") int year,
            @RequestParam(name = "month") int month
    ){
        int result = 0;
        try{
            result = monthSalaryService.deleteMonthSalary(number,year,month);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (result >= 1){
            return new Result<>(200,"success","删除成功");
        }
        else {
            return new Result<>(200,"fail","删除失败");
        }
    }

}
